﻿using System.Collections.Generic;
using UnityEngine;
using System.Linq;
using shaco.Base;
using System.Collections;

//------------------------------------------------------------
// shaco Framework
// Copyright © 2017-2021 chang.liu All rights reserved.
// Feedback: 449612236@qq.com
//------------------------------------------------------------

namespace shaco
{
    public class CoroutineHelperUnity : shaco.Base.ICoroutineHelper
    {
        Queue<IEnumerator> _willStartCoroutine = null;
        Queue<IEnumerator> _willStopCoroutine = null;

        public void StartCoroutine(IEnumerator coroutine)
        {
            if (Application.isPlaying)
            {
                GameInitComponent.StartCoroutine2(coroutine);
            }
            else
            {
                if (null == _willStartCoroutine)
                    _willStartCoroutine = new ();
                _willStartCoroutine.Enqueue(coroutine);
            }
        }

        public void StopCoroutine(IEnumerator coroutine)
        {
            if (Application.isPlaying)
            {
                GameInitComponent.StopCoroutine2(coroutine);
            }
            else
            {
                if (null == _willStopCoroutine)
                    _willStopCoroutine = new();
                _willStopCoroutine.Enqueue(coroutine);
            }
        }

        public void StopAllWillStartCoroutine()
        {
            if (null != _willStartCoroutine)
                _willStartCoroutine.Clear();
            if (null != _willStopCoroutine)
                _willStopCoroutine.Clear();
            _willStartCoroutine = _willStopCoroutine = null;
        }

        public void GetWillStartCoroutine(out IEnumerator iter)
        {
            iter = null;
            if (null == _willStartCoroutine)
                return;

            iter = _willStartCoroutine.Dequeue();
            if (0 == _willStartCoroutine.Count)
                _willStartCoroutine = null;
        }

        public void GetWillStopCoroutine(out IEnumerator iter)
        {
            iter = null;
            if (null == _willStopCoroutine)
                return;

            iter = _willStopCoroutine.Dequeue();
            if (0 == _willStopCoroutine.Count)
                _willStopCoroutine = null;
        }
    }
}